USE SoftUni
GO

DECLARE @employee_firstName NVARCHAR(30)
DECLARE @employee_lastName NVARCHAR(30)
DECLARE @employee_town NVARCHAR(30)
DECLARE cursor_employeesScan CURSOR READ_ONLY
	FOR SELECT
		e.FirstName,
		e.LastName,
		t.Name AS Town
			FROM Employees e
		JOIN Addresses a -- http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins
			ON a.AddressID = e.AddressID
		JOIN Towns t -- http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join
			ON t.TownID = a.TownID

OPEN cursor_employeesScan
FETCH NEXT FROM cursor_employeesScan INTO
	@employee_firstName,
	@employee_lastName,
	@employee_town

WHILE @@FETCH_STATUS = 0
BEGIN
	DECLARE cursor_dataPrint CURSOR READ_ONLY
		FOR	SELECT
			e.FirstName,
			e.LastName,
			t.Name
				FROM Employees e
			JOIN Addresses a
				ON a.AddressID = e.AddressID
			JOIN Towns t
				ON a.TownID = t.TownID
			WHERE t.Name = @employee_town
	
	DECLARE @employee_firstNameForPrint NVARCHAR(30)
	DECLARE @employee_lastNameForPrint NVARCHAR(30)
	DECLARE @employee_townForPrint NVARCHAR(30)
	
	OPEN cursor_dataPrint
	FETCH NEXT FROM cursor_dataPrint INTO
		@employee_firstNameForPrint,
		@employee_lastNameForPrint,
		@employee_townForPrint

	WHILE @@FETCH_STATUS = 0
	BEGIN
		PRINT CONCAT(
			@employee_townForPrint,
			': ',
			@employee_firstNameForPrint,
			' ',
			@employee_lastNameForPrint)

		FETCH NEXT FROM cursor_dataPrint INTO
			@employee_firstNameForPrint,
			@employee_lastNameForPrint,
			@employee_townForPrint
	END

	CLOSE cursor_dataPrint
	DEALLOCATE cursor_dataPrint

	FETCH NEXT FROM cursor_employeesScan INTO
		@employee_firstName,
		@employee_lastName,
		@employee_town
END

CLOSE cursor_employeesScan
DEALLOCATE cursor_employeesScan